<template>
  <page-header-wrapper>
    <page-tab-list>
      <a-card :bordered="false">
        <a-form-model class="form" ref="form" :model="form" :labelCol="{ span: 5 }" :wrapperCol="{ span: 17 }">
          <a-form-model-item label="AppLogo">
            <cyyUploadImg
              @uploadSuccess="
                (e) => {
                  form.store_app_logo = e.data
                }
              "
              @deleteImg="form.store_app_logo = ''"
              :imgSrc="form.store_app_logo"
            >
            </cyyUploadImg>
          </a-form-model-item>

          <a-form-model-item label="App背景图片">
            <cyyUploadImg
              @uploadSuccess="
                (e) => {
                  form.store_app_bg = e.data
                }
              "
              @deleteImg="form.store_app_bg = ''"
              :imgSrc="form.store_app_bg"
              imgSizeText=""
            >
            </cyyUploadImg>
          </a-form-model-item>

          <a-form-model-item label="商城端app包上传（Android）">
            <div class="flexApk">
              <a-input v-model="form.app_store_android" disabled />

              <a-upload
                class="btn"
                name="apk"
                accept=".apk"
                :action="upload_url"
                :headers="upload_headers"
                :showUploadList="false"
                @change="onUploadChange"
              >
                <a-button> <a-icon type="upload" />上传 </a-button>
              </a-upload>

              <a-button class="btn" type="danger" @click="form.app_store_android = ''"> 删除 </a-button>
            </div>
            <div style="width: 400px">
              <a-progress v-if="progress" :percent="progress" />
            </div>
          </a-form-model-item>

          <a-form-model-item label="商城端app下载地址（Ios）">
            <a-input placeholder="请输入" v-model="form.app_store_ios"> </a-input>
          </a-form-model-item>
        </a-form-model>
        <a-divider />

        <div class="tc">
          <a-button :loading="btnLoading" type="primary" @click="submitForm"> 保存 </a-button>
          <a-button @click="$router.back()" style="margin-left: 10px"> 返回 </a-button>
        </div>
      </a-card>
    </page-tab-list>
  </page-header-wrapper>
</template>

<script>
import { setAppParams, getAppParams } from '@/api/setting/wechatConfig'
import api from '@/api/storage'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { cyyUploadImg } from '@/components/CyyUi'

export default {
  components: {
    cyyUploadImg,
  },
  data() {
    return {
      btnLoading: false,
      form: {
        app_store_android: '',
        app_store_ios: '',
        store_app_bg: '',
        store_app_logo: '',
      },

      progress: 0,
      isFirst: true,
    }
  },

  computed: {
    upload_url: function () {
      const baseUrl = process.env.VUE_APP_API_BASE_URL
      return baseUrl + api.upload_app
    },
    upload_headers: function () {
      const token = storage.get(ACCESS_TOKEN)
      return { Authorization: 'Bearer ' + token }
    },
  },

  created() {
    this.getData()
  },

  methods: {
    // 上传文件改变事件
    onUploadChange(info) {
      if (info.event) {
        if (this.isFirst) this.isFirst = false
        let { total, loaded } = info.event
        this.progress = parseInt((loaded / total) * 100) - 1
      } else {
        let url = '',
          msg = ''

        for (let i = 0; i < info.fileList.length; i++) {
          if (info.fileList[i].response) {
            if (info.fileList[i].response.code === 0) {
              let data = info.fileList[i].response.data[0]
              url = data.url

              if (data.status === false) {
                msg = data.error
                this.progress = 100
                if (msg && this.progress === 100 && !this.isFirst) {
                  this.$nextTick(() => {
                    this.$message.error(msg)
                    this.isFirst = true
                    this.progress = 0
                  })
                }
                return
              }

              if (url) {
                this.$nextTick(() => {
                  this.form.app_store_android = url

                  this.progress = 100
                  setTimeout(() => {
                    this.progress = 0
                    this.isFirst = true
                  }, 200)
                })
              }
            }
          }
        }
      }
    },

    getData() {
      getAppParams().then((res) => {
        if (!res.code) {
          this.form = res.data
        }
      })
    },

    submitForm() {
      this.btnLoading = true
      setAppParams(this.form)
        .then((res) => {
          if (res.code == 0) {
            this.$message.success('保存成功！')
          } else {
            this.$message.warning(res.msg ? res.msg : '保存失败！')
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.btnLoading = false
        })
    },
  },
}
</script>

<style lang="less" scoped>
h4 {
  font-size: 16px;
  font-weight: bold;
}

.tc {
  text-align: center;
}

.ts {
  color: #ff0000;
}

.form {
  margin-top: 40px;
}

.flexApk {
  display: flex;
  align-items: center;

  .btn {
    margin-left: 20px;
  }
}
</style>
